﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using DFGK_WMS.WebAPI;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DFGJ.WMS_PDA.WebAPI.Controllers.PDA
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IUserRepository userRepository;
        private readonly ILogRepository LogService;

        public UserController(IUserRepository userRepository,ILogRepository LogService)
        {
            this.userRepository = userRepository;
            this.LogService = LogService;
        }
        [HttpPost]
        public IActionResult Login(LoginPDADTO loginPDADTO)
        {
            ApiResult apiResult = new ApiResult();
            var obj = userRepository.GetLoginPDA(loginPDADTO);
            if (obj != null)
            {
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 0;
                logg.ActionType = 1;
                logg.ModuleName = "pda登录";
                logg.UserName = obj.UserName;
                logg.RealName = obj.RealName;
                logg.UserId = obj.Id;
                logg.Description = "IP地址为" + ip + "用户名为" + loginPDADTO.LoginName + "，登录成功！";
                //获取前端IP地址
                logg.Ipaddress = ip;

                LogService.AddTLod(logg);
                //返回一个空的密码
                obj.Pwd = string.Empty;
                return Ok(BaseResult.Result(obj, "登录成功！", "200"));
            }
            apiResult.code = "500";
            apiResult.message = "账号密码有误！";
            return Ok(apiResult);
        }
    }
}
